﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Provider;
using System.Data;

namespace AccessToDataEnergyLibrury
{
    public partial class AccessDataEnergyLibrury
    {

        public List<Record> GetAllRecord()
        {
            DataRowCollection searchedRecordRow = this.energyDataSet.record.Rows;

            return this.GetRestPartsOfRecord(searchedRecordRow);
        }

        private List<Record> GetRestPartsOfRecord(DataRowCollection searchedRecordRow)
        {
            if (searchedRecordRow == null || searchedRecordRow.Count == 0)
            {
                return null;
            }
            else
            {
                List<Record> records = new List<Record>();

                foreach (energyzmemDataSet.recordRow recordRow in searchedRecordRow)
                {
                    Record record = new Record();
                    record.InitialCost = recordRow.initial_cost.ToString();
                    record.NumRecord = recordRow.num_record.ToString();
                    record.InvNum = recordRow.inv_num;
                    record.Publisher = recordRow.itmesRow.enterpriseRow.abbreviation;


                    if (recordRow.itmesRow != null)
                    {
                        record.ID = recordRow.itmesRow.id_Item;
                        record.Name = recordRow.itmesRow.name_item;
                        record.PublishedDate = recordRow.itmesRow.publisher_date_item;
                    }
                    else
                    {
                        throw new Exception("Нет данных в таблице Items для документов");
                    }

                        if (recordRow.ref_accountsRow != null)
                        {
                            record.Accounts = recordRow.ref_accountsRow.accounts;
                        }
                        else
                        {
                            throw new Exception("Нет данных в таблице Accounts для документов");
                        }


                    if (recordRow.employeeRowByFK_Record_Employee_id_Employee != null)
                    {
                        record.AdoptedRecord = recordRow.employeeRowByFK_Record_Employee_id_Employee.last_name;
                        record.PassedRecord = recordRow.employeeRowByFK_Record_Employee1_id_Employee1.last_name;
                    }
                    else
                    {
                        throw new Exception("Нет данных в таблице Employee для документов");
                    }

                        if(recordRow.ref_recordRow != null)
                        {
                            record.TypeRecord = recordRow.ref_recordRow.Record_name;
                        }
                        else
                        {
                            throw new Exception("Нет данных в таблице Ref_record для документов");
                        }
                    records.Add(record);
                }
                return records;
            }
        }
    }
}
